package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.PersonInfoEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.personmanger.PersonMangerActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonMangerModel {
    private PersonMangerActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public PersonMangerModel(PersonMangerActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void refresh(Map map) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getOrderAPI().personList(map).enqueue(new Callback<BaseEntity<List<PersonInfoEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PersonInfoEntity>>> call, Response<BaseEntity<List<PersonInfoEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getData() != null)
                    if (response.body().getData().size() != 0) {
                        activity.refresh(response.body().getData());
                    }else {
                        activity.empty();
                    }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PersonInfoEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void personDelete(Map map, int position) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getOrderAPI().personDelete(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if ("ok".equals(response.body().getState())) {
                    activity.deleteOk(position);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}
